package com.hongyu.smart.village.ui.activity;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hongyu.smart.village.R;
import com.hongyu.smart.village.event.Event;
import com.hongyu.smart.village.listener.OnItemClickListener;
import com.hongyu.smart.village.model.entity.BaseResponse;
import com.hongyu.smart.village.model.response.MemberResp;
import com.hongyu.smart.village.network.BaseObserver;
import com.hongyu.smart.village.network.RetrofitHelper;
import com.hongyu.smart.village.network.RxUtil;
import com.hongyu.smart.village.network.api.AppInfoApi;
import com.hongyu.smart.village.ui.adapter.MemberAdapter;
import com.hongyu.smart.village.ui.base.BaseToolbarActivity;
import com.hongyu.smart.village.util.SPUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MemberActivity extends BaseToolbarActivity {
    @BindView(R.id.member_recycle_view)
    RecyclerView recyclerView;
    MemberAdapter adapter;
    List<MemberResp> list=new ArrayList<>();
    int memberId=0;
    private String number="";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public String getToolBarTitle() {
        return "成员管理";
    }

    @Override
    protected void initActivityData() {

        memberList();
    }

    @Override
    protected void initContentView() {
        adapter=new MemberAdapter(this,list);
        adapter.setmOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                memberId=list.get(position).getId();
                getNextPage();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        recyclerView.setAdapter(adapter);

    }

    private void getNextPage() {
        mBundle=getBundle();
        if (mBundle==null){
            mBundle=new Bundle();
        }
        mBundle.putInt("MEMBERID",memberId);
        sendIntent(MemberEditActivity.class,mBundle);
    }

    private void memberList() {
        number= SPUtil.getFamilyNumber();
        RetrofitHelper.create(AppInfoApi.class)
                .memberList(number)
                .compose(RxUtil.observervableToMain())
                .as(RxUtil.bindLifecycle( this))
                .subscribe(new BaseObserver<List<MemberResp>>(getLoadingDialog()){
                    @Override
                    public void onSuccess(BaseResponse<List<MemberResp>> response) {
                        if (response.getData()!=null) {
                            list.addAll(response.getData());
                            adapter.notifyDataSetChanged();
//                            ToastTool.show(""+adapter.getItemCount());







                        }
                    }
                });

    }
    @OnClick({R.id.btn_add})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.btn_add:
                memberId=0;
                getNextPage();
                break;
        }
    }
    @Override
    protected int getContentView() {
        return R.layout.activity_member;
    }

    @Override
    protected void onEventProcess(Event event) {

    }
}